<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<script type="text/javascript">
	var positionDataGrid = $('#position-datagrid-json').datagrid({
	    gridPosition : '职位管理',
	    showToolbar: true,
	    toolbarItem: '',
	    showLinenumber: false,
	    filterThead: false,
	    filterAll: true,
	    sortAll : true,
	    dataUrl: 'position/list.do',
	    dataType: 'json',
	    columns: [
	    	{
				name: 'status',
				label: '操作',
				align: 'center',
				width: 190,
				quicksort:false,
				menu: false,
				edit: false,
				render: function(value){
			        var html = '';
				    if(value == 0){
						html += '<div class="btn-group" role="group"><button type="button" class="btn btn-green btn-sm" onclick="changePositionStatus(this,1)"><i class="fa fa-edit"></i> 发布</button></div>';
					}else{
						html += '<div class="btn-group" role="group"><button type="button" class="btn btn-orange btn-sm" onclick="changePositionStatus(this,0)"><i class="fa fa-edit"></i> 取消</button></div>';
					}
			    	html += '<div class="btn-group" role="group"><button type="button" class="btn btn-blue btn-sm" onclick="editPosition(this)"><i class="fa fa-edit"></i> 编辑</button></div>';
				    html += '<div class="btn-group" role="group"><button type="button" class="btn btn-red btn-sm" onclick="delPosition(this)"><i class="fa fa-remove"></i> 删除</button></div>';
				    return html;
				}
	        },
	        {
	            name: 'positionCode',
	            label: '职位编码',
	            align: 'center',
	            width: 120
	        },
	        {
	            name: 'positionName',
	            label: '职位名称',
	            align: 'center',
	            width: 120
	        },
	        {
	            name: 'status',
	            label: '状态',
	            type : 'select',
	            items: [{'0':'未发布'}, {'1':'已发布'}],
	            align: 'center',
	            width: 80,
	            render: $.datagrid.renderItem
	        },
	        {
	            name: 'positionDesc',
	            label: '职位描述',
	            align: 'center',
	            width: 380
	        }
	    ],
	    afterSave: function($trs, datas) {
	        	this.refresh();
	        },
	    paging: {pageSize:20, selectPageSize:'20,30,40'},
	    showTfoot: true,
	    fullGrid: false,
	    toolbarCustom: function(){
	    	var html = '';
	    	html += '<div class="btn-group" role="group"><button type="button" class="btn btn-blue" onclick="addPosition()"><i class="fa fa-plus"></i> 添加</button></div>';
		    html += '<div class="btn-group" role="group"><button type="button" class="btn btn-green" onclick="refreshPosition()"><i class="fa fa-refresh"></i> 刷新</button></div>';
		    return html;
	    }
	});

	function changePositionStatus(_btn, status){
		var trIndex = $(_btn).closest("tr").index();
		var data = positionDataGrid.data('bjui.datagrid').getRowData(trIndex);
		var tips = "";
		if(status == 1){
			tips = "确定发布当前数据？";
		} else if (status == 0) {
			tips = "确定取消发布当前数据？";
		}
		positionDataGrid.alertmsg('confirm', tips, 
				{
				    displayMode:'slide', 
		            okName:'确定', 
		            cancelName:'取消', 
		            position:'提示信息',
		            okCall : function(){
			            var url = "position/changeStatus.do?status="+status+"&positionSn="+data.positionSn;
						var successCallBack = function(){
							refreshPosition();
						}
						myAjax(url,	null, null,successCallBack,null);
		            }
				});
	}
	
	function refreshPosition() {
		positionDataGrid.datagrid("refresh");
	}

	var positionIndex=-1;
	function addPosition() {
		positionIndex = -1;
		$('#edit-position-dialog').dialog({
			id:'add-position-dialog', 
			url:'position/add.do', 
			maxable: false,
			mask: true,
			width: 850,
			height: 300,
			onLoad: function($dialog){
				setEditPositionBtnEvent($dialog, true);
			},
			title:'职位管理 - 添加'});
	}
	function editPosition(_this) {
		positionIndex = $(_this).closest("tr").index();
		var data = positionDataGrid.data('bjui.datagrid').getRowData(positionIndex);
		$('#edit-position-dialog').dialog({
			id:'edit-position-dialog',
			url:'position/edit.do',
			maxable: false,
			mask: true,
			width: 850,
			height: 300,
			onLoad: function($dialog){
				setEditPositionBtnEvent($dialog, false);
				getJsonData2Form('position/getPosition.do', 'positionSn='+data.positionSn, $('#position_form'), positionCallback);
			},
			
			title:'职位管理 - 编辑'});
	}
	function delPosition(_btn){
		positionIndex = $(_btn).closest("tr").index();
		var data = positionDataGrid.data('bjui.datagrid').getRowData(positionIndex);

		positionDataGrid.alertmsg('confirm', '确定删除当前数据？', 
			{
			    displayMode:'slide', 
	            okName:'确定', 
	            cancelName:'取消', 
	            position:'提示信息',
	            okCall : function(){
	            	var url = "position/del.do?positionSn="+data.positionSn;
					var successCallBack = function(){
						refreshPosition();
					}
					myAjax(url,	null, null,successCallBack,null);
	            }
	    });
	}
	 function positionCallback($inputObj, _name, _val){
		$("#l_positionCode").attr("readonly","readonly");
	}
	
	function setEditPositionBtnEvent($dialog, isAdd) {
		var $save, $cancel;
		$save = $dialog.find('.save');
		$cancel = $dialog.find('.cancel');
	
		$save.click(function(){
			$('#position_form').isValid(function(v) {
				if(v){
					var url = 'position/save.do';
					var data = $('#position_form').serialize()+"&addFlag="+isAdd;
					var successCallBack = function(){
						$dialog.dialog('closeCurrent');
						refreshPosition();
					}
					myAjax(url,	data, true,successCallBack,null);
				}
			});
		});
	
		$cancel.click(function(){
			$dialog.dialog('closeCurrent');
		});
	}
</script>
<div class="bjui-pageContent" id="position-index-pagecontent">
    <div style="padding:0px; height:100%; width:100%;">
        <table id="position-datagrid-json" data-width="100%" data-height="100%" class="table table-bordered">
        </table>
    </div>
</div>
<div id="edit-position-dialog" style="display: none;">
</div>
<div id="set-password-dialog" style="display: none;">
</div>